package com.whfc.hdc.service.impl;

import com.whfc.common.exception.BizException;
import com.whfc.common.util.AesCryptUtil;
import com.whfc.hdc.dao.FshDeviceConfigMapper;
import com.whfc.hdc.dao.FshWarnRuleMapper;
import com.whfc.hdc.dto.FshDeviceConfigDTO;
import com.whfc.hdc.dto.FshWarnRuleDTO;
import com.whfc.hdc.entity.FshDeviceConfig;
import com.whfc.hdc.manager.FshDeviceDataManager;
import com.whfc.hdc.params.FshDeviceConfigParam;
import com.whfc.hdc.params.FshDeviceInitParam;
import com.whfc.hdc.service.FshDeviceConfigService;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-04-07 15:58
 */
@DubboService(interfaceClass = FshDeviceConfigService.class, version = "1.0.0")
public class FshDeviceConfigServiceImpl implements FshDeviceConfigService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FshDeviceConfigMapper deviceConfigMapper;

    @Autowired
    private FshWarnRuleMapper warnRuleMapper;

    @Autowired
    private FshDeviceDataManager fshDeviceDataManager;


    @Override
    public void initDeviceConfig(FshDeviceInitParam param) throws BizException {
        logger.info("初始化设备配置, param:{}", param);
        List<String> list = param.getSnList();
        for (String sn : list) {
            fshDeviceDataManager.initDeviceConfig(sn);
        }
    }

    @Override
    public FshDeviceConfigDTO getDeviceConfig(String sn) throws BizException {
        logger.info("获取设备配置, sn:{}", sn);
        FshDeviceConfig fshDeviceConfig = deviceConfigMapper.selectBySn(sn);
        if (fshDeviceConfig == null) {
            fshDeviceDataManager.initDeviceConfig(sn);
            fshDeviceConfig = deviceConfigMapper.selectBySn(sn);
        }
        FshDeviceConfigDTO configDTO = new FshDeviceConfigDTO();
        if (fshDeviceConfig != null) {
            //时间配置
            configDTO.setPosTimes(fshDeviceConfig.getPosTimes());
            configDTO.setVoiceTimes(fshDeviceConfig.getVoiceTimes());
            configDTO.setVideoTimes(fshDeviceConfig.getVideoTimes());
            //请求地址配置
            configDTO.setRequestHost(fshDeviceConfig.getRequestHost());
            //第三方信息配置 使用AES加密将敏感信息加密  加密key为  当前 sn
            String key = (sn + "0000000000").substring(0, 16);
            String tcAppId = AesCryptUtil.encryptEcb(fshDeviceConfig.getTcAppId(), key);
            String jpushAppKey = AesCryptUtil.encryptEcb(fshDeviceConfig.getJpushAppKey(), key);
            String jpushSecret = AesCryptUtil.encryptEcb(fshDeviceConfig.getJpushSecret(), key);
            configDTO.setTcAppId(tcAppId);
            configDTO.setJpushAppKey(jpushAppKey);
            configDTO.setJpushSecret(jpushSecret);
        }
        return configDTO;
    }

    @Override
    public void setDeviceConfig(FshDeviceConfigParam param) throws BizException {
        logger.info("设置设备配置, params:{}", param);
        String sn = param.getSn();
        FshDeviceConfig fshDeviceConfig = deviceConfigMapper.selectBySn(sn);
        Integer posTimes = param.getPosTimes();
        Integer voiceTimes = param.getVoiceTimes();
        Integer videoTimes = param.getVideoTimes();
        if (fshDeviceConfig == null) {
            //创建配置
            fshDeviceConfig = new FshDeviceConfig();
            fshDeviceConfig.setDeviceId(param.getDeviceId());
            fshDeviceConfig.setSn(sn);
            fshDeviceConfig.setPosTimes(posTimes);
            fshDeviceConfig.setVoiceTimes(voiceTimes);
            fshDeviceConfig.setVideoTimes(videoTimes);
            deviceConfigMapper.insertSelective(fshDeviceConfig);
        } else {
            fshDeviceConfig.setPosTimes(posTimes);
            fshDeviceConfig.setVoiceTimes(voiceTimes);
            fshDeviceConfig.setVideoTimes(videoTimes);
            deviceConfigMapper.updateByPrimaryKeySelective(fshDeviceConfig);
        }
    }

    @Override
    public List<String> getRegistrationIdList(List<String> snList) throws BizException {
        logger.info("获取极光推送注册ID列表, snList:{}", snList);
        List<String> registrationIdList = new ArrayList<>();
        if (snList == null || snList.isEmpty()) {
            return registrationIdList;
        }
        registrationIdList = deviceConfigMapper.selectRegistrationIdList(snList);
        return registrationIdList;
    }

    @Override
    public void savePushRegistrationId(Integer deviceId, String sn, String registrationId) throws BizException {
        logger.info("保存极光推送注册ID, sn:{}, registrationId:{}", sn, registrationId);
        FshDeviceConfig fshDeviceConfig = deviceConfigMapper.selectBySn(sn);
        if (fshDeviceConfig == null) {
            fshDeviceConfig = new FshDeviceConfig();
            fshDeviceConfig.setDeviceId(deviceId);
            fshDeviceConfig.setSn(sn);
            fshDeviceConfig.setRegistrationId(registrationId);
            deviceConfigMapper.insertSelective(fshDeviceConfig);
        } else {
            fshDeviceConfig.setRegistrationId(registrationId);
            deviceConfigMapper.updateByPrimaryKeySelective(fshDeviceConfig);
        }
    }

    @Override
    public List<FshWarnRuleDTO> getWarnRuleList() throws BizException {
        logger.info("获取报警类型列表");
        return warnRuleMapper.selectAll();
    }
}
